﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NigiriClub.WebUI.Models;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace NigiriClub.WebUI.Controllers
{
    public class ProfileController : Controller
    {
        //
        // GET: /Profile/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult EditSubmit(Account account)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    Account user = db.Account.FirstOrDefault(u => u.Name == account.ExName);
                    user.Level = account.Level;
                    user.Name = account.Name;
                    user.FullName = account.FullName;
                    // Check if user already exists
                    db.SaveChanges();
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
            return RedirectToAction("Edit", account);
        }

        [HttpGet]
        public ActionResult Edit(Account account)
        {
            return View(account);
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult ProfileLink(string name)
        {
            Account user;

            using (Entities db = new Entities())
            {
                user = db.Account.FirstOrDefault(u => u.Name == name);
            }
            return PartialView("_ProfileLink", user.Clone());
        }

        [AllowAnonymous]
        [ChildActionOnly]
        public ActionResult ProfileLinkById(int id)
        {
            Account user;

            using (Entities db = new Entities())
            {
                user = db.Account.FirstOrDefault(u => u.Id == id);
            }

            return PartialView("_ProfileLink", user.Clone());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadAvatar(Account account)
        {
            string fileName = String.Format("{0}-{1}", Guid.NewGuid().ToString().Substring(0, 13),
                account.AvatarFile.file.FileName);
            string fileRelativePath = String.Format("/Content/{0}/{1}",
                "images/avatars", fileName);
            string filePath = Server.MapPath(fileRelativePath);

            account.AvatarFile.file.SaveAs(filePath);

            using (Entities db = new Entities())
            {
                Account updated = db.Account.FirstOrDefault(u => u.Id == account.Id);
                updated.Avatar = fileName;
                db.SaveChanges();
            }

            return RedirectToAction("Edit");
        }
    }
}
